<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String code = request.getParameter("code"); 
String contextPath = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<jsp:include page="/inc/inc.jsp"></jsp:include>
<script type="text/javascript">
/**
 *  检验字段是否以param为结尾
 * @param param
 * @returns {Boolean}
 */
 
String.prototype.endWith = function (param) { 
      if (param == null || param == "" || this.length == 0 || param.length > this.length) { 
        return false; 
      } 
      if (this.substring(this.length - param.length) == param) { 
        return true; 
      } else { 
        return false; 
      } 
};

/**
 * 手动和字典页签可以共存，但伪页签不能和手动、字典页签共存
 */
$(function(){
	var curTabTitle_ = "";
	$.ajax({
		url:'iframeAction.do?method=getIframes',
		data:{code:'<%=code%>'},
		type:'post',
		dataType: 'json',
		success: function(data){
	    	var objs = data;
	    	for(var i=0;i<objs.length;i++){
	    		//字典页签
	    		var iframeId=objs[i].id;
	    		var datasrcId = objs[i].datasrcId;
	    		if(objs[i].typeId==2){
	    			var method='';
		    		if(objs[i].dicCode.indexOf("dm")!=-1){
		    			method='loadDic';
		    		}else{
		    			method='loadTabm';
		    		}
		    		var url=objs[i].url;
		    		var code = objs[i].dicCode;
		    		$.ajax({
	    				url:'iframeAction.do?method='+method,
	    				data:{code:code},
	    				type:'post',
	    				dataType:'json',
	    				success:function(obj){
	    					for(var j=0;j<obj.length;j++){			
	    						var baseurl = '<%=contextPath%>/'+url;
	    						if(baseurl.endWith("jsp")){
	    							baseurl = baseurl+"?tabId_="+obj[j].id+"&iframeId_="+iframeId+"&datasrcId_="+datasrcId;
	    						}else{
	    							baseurl = baseurl+"&tabId_="+obj[j].id+"&iframeId_="+iframeId+"&datasrcId_="+datasrcId;
	    						}
	    						var title = obj[j].text;
	    						if(title.indexOf("-")!=-1){
	    							var titles =title.split("-");
	    							title = titles[0];
	    						}
	    						var selected =false;
	    						if(j==0){
	    							selected=true;
	    							curTabTitle_=title;
	    						}
	    						$('#tabs').tabs('add',{
	    			    			title:title,
	    			    			selected:selected,
	    			    			content:'<iframe scrolling=\"auto\" frameborder=\"0\"  src=\"'+baseurl+'\" style=\"width:100%;height:100%;\"></iframe>',
	    			    			closable:false,
	    			    			cache:true
	    			    		});
	    						
	    					}
	    				}
	    			});	
		    	}else if(objs[i].typeId==1){ //手动页签
		    		var selected =false;					
					var tabid = objs[i].tabId;
		    		if(objs[i].tabId==null||objs[i].tabId=='')
		    			tabid=objs[i].id;
					var baseurl = '<%=contextPath%>/'+objs[i].url;
					if(baseurl.endWith("jsp")){
						baseurl = baseurl+"?tabId_="+tabid+"&iframeId_="+iframeId+"&datasrcId_="+datasrcId;
					}else{
						baseurl = baseurl+"&tabId_="+tabid+"&iframeId_="+iframeId+"&datasrcId_="+datasrcId;
					}
					var title = objs[i].title;					
					if(title.indexOf("-")!=-1){
						var titles =title.split("-");
						title = titles[0];
					}
					if(i==0){
						selected=true;
						curTabTitle_=title;
					}
		    		$('#tabs').tabs('add',{
		    			title:title,
		    			selected:selected,
		    			content:'<iframe scrolling=\"auto\" frameborder=\"0\"  src=\"'+baseurl+'\" style=\"width:100%;height:100%;\"></iframe>',
		    			closable:false,
		    			cache:true
		    		});
		    	}else{ //伪页签
		    		var url = '<%=contextPath%>/'+objs[i].url;
		    		var tabid = objs[i].tabId;
		    		if(objs[i].tabId==null||objs[i].tabId=='')
		    			tabid=objs[i].id;
		    		if(url.endWith(".jsp")){		    				
		    			self.location.href=url+'?tabId_='+tabid+"&iframeId_="+iframeId+"&datasrcId_="+datasrcId;;
		    		}else{
		    			self.location.href=url+'&tabId_='+tabid+"&iframeId_="+iframeId+"&datasrcId_="+datasrcId;
		    		}
		    		break;
		    	}
	    	}
	    }
	});
	$('#tabs').tabs({
	       border:false,
	       onSelect:function(title){
	    	   if(title!=curTabTitle_){
	    		   curTabTitle_=title;
		           var currTab = $('#tabs').tabs('getSelected');
		           url=$(currTab.panel('options').content).attr('src');
		           $('#tabs').tabs('update',{
						tab:currTab,
						options:{
							content:'<iframe scrolling=\"auto\" frameborder=\"0\"  src=\"'+url+'\" style=\"width:100%;height:100%;\"></iframe>'
						}
					});
	       		}
			}
	});
	
});

</script>
</head>
<body>
<div id="tabs" class="easyui-tabs" fit="true" border="false">

</div>
</body>
</html>